package com.psct.social;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.psct.ThreadPool;
import com.tencent.weibo.api.UserAPI;
import com.tencent.weibo.constants.OAuthConstants;
import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.webview.OAuthV2AuthorizeWebView;

/**
 * �����ʺſ��ƹ�����
 * 
 * @author zjingchuan
 * 
 */
public class SocialManager {
	private static SocialManager socialManager;
	private SocialAccount sinaAccount;
	private SocialAccount tenceAccount;
	private Context context;

	public SocialAccount getSinaAccount() {
		return sinaAccount;
	}

	public void setSinaAccount(SocialAccount sinaAccount) {
		this.sinaAccount = sinaAccount;
	}

	public SocialAccount getTenceAccount() {
		return tenceAccount;
	}

	public void setTenceAccount(SocialAccount tenceAccount) {
		this.tenceAccount = tenceAccount;
	}

	private SocialManager(Context context) {
		this.context = context;
		sinaAccount = SocialAccountKeeper.getUserInfo(context, "sina");
		tenceAccount = SocialAccountKeeper.getUserInfo(context, "tence");
	}

	public static SocialManager getInstance(Context context) {
		if (socialManager == null) {
			socialManager = new SocialManager(context);
		}
		return socialManager;
	}

	public boolean isSinaBinded() {
		sinaAccount = SocialAccountKeeper.getUserInfo(context, "sina");
		return sinaAccount.isBind;
	}

	public boolean isTenceBinded() {
		tenceAccount = SocialAccountKeeper.getUserInfo(context, "tence");
		return tenceAccount.isBind;
	}

	public void bindingSina() {
		sinaAccount.bind(context, null);
	}

	public void bindingSina(Activity activity,OnBindListener listener) {
		sinaAccount.bind(activity, listener);
	}

	public void bindingTence() {
		tenceAccount.bind(context, null);
	}

	public void bindingTence(Activity activity,OnBindListener listener) {
		tenceAccount.bind(activity, listener);
	}

	public List<SocialAccount> getAccountList() {
		List<SocialAccount> list = new ArrayList<SocialAccount>();
		list.add(sinaAccount);
		list.add(tenceAccount);
		return list;
	}

	public void updateAccountList(List<SocialAccount> list) {
		list.clear();
		sinaAccount = SocialAccountKeeper.getUserInfo(context, "sina");
		tenceAccount = SocialAccountKeeper.getUserInfo(context, "tence");
		list.add(sinaAccount);
		list.add(tenceAccount);
	}

	/**
	 * 绑定腾讯微博后的结果处理
	 * 
	 * @param requestCode
	 * @param resultCode
	 * @param data
	 */
	public void handlActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == TenceAccount.REQUEST_CODE) {
			if (resultCode == OAuthV2AuthorizeWebView.RESULT_CODE) {
				OAuthV2 oAuth = (OAuthV2) data.getExtras().getSerializable("oauth");
				if (oAuth.getStatus() == 0) {
					SocialAccountKeeper.keepAccessToken(context, oAuth);
					ThreadPool.execute(new Runnable() {

						@Override
						public void run() {
							UserAPI userAPI = new UserAPI(OAuthConstants.OAUTH_VERSION_2_A);
							try {
								String result = userAPI.info(
										SocialAccountKeeper.getTenceOAuth(context), "json");
								JSONObject jsonObject = new JSONObject(result)
										.getJSONObject("data");
								String name = jsonObject.getString("nick");
								String headImage = jsonObject.getString("head") + "/50";
								tenceAccount.handlUsernfo(context, name, headImage, "tence");
							} catch (Exception e) {
								e.printStackTrace();
							}

						}

					});

				}
			}
		}
	}

	/**
	 * ��֮��Ļص��ӿ�
	 * 
	 * @author zjingchuan
	 * 
	 */
	public interface OnBindListener {
		void onBind();
	}
}
